import { Inject, OnApplicationShutdown } from "@nestjs/common";
import { REQUEST } from "@nestjs/core";
import { DataSource } from "typeorm";

export class TypeOrmProvider implements OnApplicationShutdown {

    constructor(
        @Inject(REQUEST) private request: Request,
        @Inject('TYPEORM_CONNECTS') private connections: Map<string, DataSource>
    ) { }

    // 用于在app关闭时销毁多数据库连接共用实列
    onApplicationShutdown(signal?: string) {
        if (this.connections.size > 0) {
            for (const key of this.connections.keys()) {
                this.connections.get(key)?.destroy()
            }
        }
    }

}